package core;
import java.util.*;
import core.exceptions.*;

/** 
 * An equilibrium represents a relationship between species and components
 *
 * @author Charlie Moore
 */
public class Equilibrium {
    
    /**
     * A link to the Chemical system.
     *
     * The Equilibrium must know the total list of components in the system to
     * parse the equation.
     */
    private ChemicalSystem system; 

    /**
     * A list of the species in this equilibrium
     */
    private Vector<Specie> species = new Vector<Specie>();

    /**
     * A list of the components in this equilibrium
     */
    private Vector<Component> components = new Vector<Component>();

    /**
     * A string holding the original equation before parsing
     */
    private String equation;
    
    /**
     * The equilibrium constant
     */
    private double k;
    
    //=================================
    // Getters and Setters
    //---------------------------------
    
    public Vector<Specie> getSpecies() { return species; }
    
    public Vector<Component> getComponents() { return components; }

    public String getEquation() {
        return equation;
    }

    public double getK()
    {
        return k;
    }
    
    //=================================
    // Real Methods
    //---------------------------------

    /**
     * Constructor for an Equilibrium.
     *
     * @param sys the system that is relevant to this equilibrium
     * @param newEquation an equation that represents this equilibrium
     */
    public Equilibrium(ChemicalSystem sys, String newEquation, double k) {
        system = sys;
        equation = newEquation;
        this.k = k;
    }

    public Equilibrium(String newEquation, double k) {
        equation = newEquation;
        this.k = k;
    }
    
    /**
     * Get Chemical Entity names from a side of an equation
     */
    private String[] getEntityNames(String side) {
        String[] entities = side.split("[+-]");
        for( String name : entities) {
            name.trim();
        }
        return entities;
    }

    /**
     * Parses <code>equation</code> and generates a list of Species and a 
     * list of Components as a side effect.
     * 
     * @see #equation
     */
    public void parseEquation() throws InvalidEquation {
        String[] parseList = equation.split("\u21cc");
        String LHS = parseList[0].trim();
        String RHS = parseList[1].trim();
        boolean found = false;

        // Make the entities on the LHS components
        for (String name : getEntityNames(LHS)) {
            this.components.add(new Component(name.trim()));
        }

        // Check that there's only one unknown on the RHS
        for (String name : getEntityNames(RHS)) {
            for (Component comp : this.components) {
                if (name == comp.getName()) {
                    found = true;
                    break;
                }
            }
            this.species.add(new Specie(name.trim()));
        }

        if (species.size() < 1) {
            throw new InvalidEquation("Too few Species");
        }
        else if (species.size() > 1) {
            // Ignore this case, the user should take care of it.
            species.clear();
            return;
        }

        // Rearrange the equation to move all non species to the left
        Specie spec = species.get(0);
        for (String name : getEntityNames(RHS)) {
            if (name != spec.getName()) {
            }
        }

    }

}
